/**
  对Date的扩展，将 Date 转化为指定格式的String
  月(M)、日(d)、小时(h)、分(m)、秒(s)、季度(q) 可以用 1-2 个占位符，
  年(y)可以用 1-4 个占位符，毫秒(S)只能用 1 个占位符(是 1-3 位的数字)
  例子：
  (new Date()).Format("yyyy-MM-dd hh:mm:ss.S") ==> 2006-07-02 08:09:04.423
  (new Date()).Format("yyyy-M-d h:m:s.S")      ==> 2006-7-2 8:9:4.18
 * @param fmt
 */
export const dateFormat =function(fmt,d){
    //author: meizz
        var o = {
            "M+" : d.getMonth()+1,                 //月份
            "d+" : d.getDate(),                    //日
            "h+" : d.getHours(),                   //小时
            "m+" : d.getMinutes(),                 //分
            "s+" : d.getSeconds(),                 //秒
            "q+" : Math.floor((d.getMonth()+3)/3), //季度
            "S"  : d.getMilliseconds()             //毫秒
        };
        if(/(y+)/.test(fmt))
            fmt=fmt.replace(RegExp.$1, (d.getFullYear()+"").substr(4 - RegExp.$1.length));
        for(var k in o)
            if(new RegExp("("+ k +")").test(fmt))
                fmt = fmt.replace(RegExp.$1, (RegExp.$1.length==1) ? (o[k]) : (("00"+ o[k]).substr((""+ o[k]).length)));
        return fmt;
}
/**
 * 获取当前的日期
 * @returns {string}
 */
export  const getNow = function(){
     var nowTime = new Date();
     let m = nowTime.getMonth()+1
     return nowTime.getFullYear() + "-" + m + "-" + nowTime.getDate();

}
/**
 * 获取当前的日期 yyyy-MM
 * @returns {string}
 */
export  const getMonthNow = function(){
    var nowTime = new Date();
    let m = nowTime.getMonth()+1
    return nowTime.getFullYear() + "-" +  m;

}
/**
 * 获取一年中的第几周
 */
export const getWeekNumber = function(){
    var day11 = new Date();
    day11.setMonth(0);
    day11.setDate(1);
    day11.setHours(0);
    day11.setMinutes(0);
    day11.setSeconds(0);//到这里就得到该年的一月一日

    var day11mill=day11.getTime();
    var ori_day=day11.getDay();//该年的一月一日是星期几
    var fill1=0;//与星期日相隔的毫秒数
    if(ori_day!==0){
        fill1=ori_day*60*60*24*1000;
    }

    var now= new Date();

    now.setHours(0);
    now.setMinutes(0);
    now.setSeconds(0);
    var nowmill=now.getTime();
    var now_day=now.getDay();
    var fill2=0;
    if(now_day!==0){
        fill2=(7-now_day)*60*60*24*1000;
    }

    var cha2=(nowmill-day11mill+fill1+fill2)/(60*60*24*1000);
    var week=Math.ceil(cha2/7);

    return week;

}

export const getWeekOfYear = function(){
      var today = new Date();
      var firstDay = new Date(today.getFullYear(),0, 1);
      var dayOfWeek = firstDay.getDay(); 
      var spendDay= 1;
      if (dayOfWeek !=0) {
        spendDay=7-dayOfWeek+1;
      }
      firstDay = new Date(today.getFullYear(),0, 1+spendDay);
      var d =Math.ceil((today.valueOf()- firstDay.valueOf())/ 86400000);
      var result =Math.ceil(d/7);
      return result+1;
    };
/**
 * 获取当年的所有星期数
 * @returns {Array}
 */
export const  getWeeks =function(){
    var week_arr = [];
    var today=new Date;
    var nowYear =today.getFullYear();
    for(var i=12; i>0; i--){
        week_arr.unshift(getInfo(nowYear, i));
    }
    //定义空字符串
    var str_week = [];
    for(var j=week_arr.length-1; j>=0; j--){
        for(var o=0; o<week_arr[j].length; o++){
            str_week.unshift(week_arr[j][o]);
        }
    }
    var option_list = [];
    for(var f=0;f<str_week.length-1;  f++){
        // var option = '<option from='+str_week[f].split("|")[0]+" to="+str_week[f].split("|")[1]+" type=2 ext="+(f+1)+">第"+(f+1)+"周 "+ YMDinfo(str_week[f]) +'</option> \r\n';
        let option = {index:f+1,beginDate:str_week[f].split('|')[0],endDate:str_week[f].split('|')[1],showText:"第"+isTwo(f+1)+"周 "+ YMDinfoText(str_week[f])}
        option_list.push(option);
    }

    return option_list;
}
function getInfo(year, month) {
    var zhou_arr = [];
    var d = new Date();
    // what day is first day
    d.setFullYear(year, month-1, 1);
    var w1 = d.getDay();
    if (w1 == 0) w1 = 7;
    // total day of month
    d.setFullYear(year, month, 0);
    var dd = d.getDate();
    // first Monday
    let d1 = 1;
    if (w1 != 1) {
        d1 = 7 - w1 + 2;
    }
    let week_count = Math.ceil((dd-d1+1)/7);
    //document.write(year + "年" + month + "月有" + week_count +"周<br/>");
    let firstFrom = year+"-1-1";
    let firsTo = year + "-1-" +String(7-w1+1);
    if (w1 != 1 && month ==1) {
        zhou_arr.unshift(firstFrom+"|"+firsTo);
    }

    for (var i = 0; i < week_count; i++) {
        var monday = d1+i*7;
        var sunday = monday + 6;
        var from = year+"-"+month+"-"+monday;
        var to;
        if (sunday <= dd) {
            to = year+"-"+month+"-"+sunday;
        } else {
            d.setFullYear(year, month-1, sunday);
            to = d.getFullYear()+"-"+(d.getMonth()+1)+"-"+d.getDate();
        }
        if(i == week_count -1 && month ==12){
            to = year+ "-12-31";
        }
        zhou_arr.unshift(from + "|" + to);
        //document.write("第"+(i+1)+"周 从" + from + " 到 " + to + "<br/>");
    }

    return zhou_arr;
}
function YMDinfo(ymd){
    var arr = new Array();
    var arr = ymd.split("|");
    return arr[0].split("-")[1] + "月" + arr[0].split("-")[2] + "日" + "-" + arr[1].split("-")[1] + "月" + arr[1].split("-")[2] + "日";
}
function YMDinfoText(ymd){
    var arr = new Array();
    var arr = ymd.split("|");
    return isTwo(arr[0].split("-")[1]) + "月" + isTwo(arr[0].split("-")[2]) + "日" + "-" + isTwo(arr[1].split("-")[1])+ "月" + isTwo(arr[1].split("-")[2]) + "日";
}
function isTwo(text) {
    if(text > 9){
        return text;
    }else{
        return '0'+text;
    }
}